package model

import (
	"time"

	"bilinadmin/pkg/util"
)

const (
	// OpStatusInviting 待确认
	OpStatusInviting int8 = 1
	// OpStatusValid 运营角色生效中
	OpStatusValid int8 = 2
	// OpStatusInvalid 运营角色失效
	OpStatusInvalid int8 = 3

	OpStatusInvitingStr string = "inviting"
	OpStatusValidStr           = "valid"
	OpStatusInvalidStr         = "invalid"
)

// Operator 运营人员
type Operator struct {
	// id
	Id int64 `json:"id" sql:"id"`
	// 品牌id
	BrandId int64 `json:"brand_id" sql:"brand_id"`
	// 对应实体id
	RefId int64 `json:"ref_id" sql:"ref_id"`
	// 对应实体类型 0~品牌，1~门店， 2~仓库
	RefType int8 `json:"ref_type" sql:"ref_type"`
	// 员工id
	EmpId int64 `json:"emp_id" sql:"emp_id"`
	// 角色id
	RoleId int64 `json:"role_id" sql:"role_id"`
	// 状态 0~正常，1~邀请中，3~离职
	Status int8 `json:"status" sql:"status"`
	// 角色之外权限
	CustomPerSet []byte `json:"custom_per_set" sql:"custom_per_set"`
	// 创建者id
	ModifyEmpId int64     `json:"modify_emp_id" sql:"modify_emp_id"`
	CreatedAt   time.Time `json:"created_at" sql:"created_at"` // 创建时间
	UpdatedAt   time.Time `json:"updated_at" sql:"updated_at"` // 更新时间
	DeletedAt   int64     `json:"delete_at" sql:"deleted_at"`  // 删除时间
}

func NewOperator(brandId, refId, empId, roleId, creator int64, refType, status int8) *Operator {
	now := util.GetNow()
	return &Operator{
		BrandId:      brandId,
		RefId:        refId,
		RefType:      refType,
		EmpId:        empId,
		RoleId:       roleId,
		Status:       status,
		CustomPerSet: []byte("{}"),
		ModifyEmpId:  creator,
		CreatedAt:    now,
		UpdatedAt:    now,
	}
}

// IsValid 是否有效
func (o *Operator) IsValid() bool {
	return o.Status == OpStatusValid && o.DeletedAt == 0
}

// IsInviting 邀请中
func (o *Operator) IsInviting() bool {
	return o.Status == EmployeeStatusInviting && o.DeletedAt == 0
}

// Enable 设置为正常状态
func (o *Operator) Enable(modifier int64) {
	o.Status = OpStatusValid
	o.ModifyEmpId = modifier
	o.UpdatedAt = util.GetNow()
}

// Invalid 离职
func (o *Operator) Invalid(modifier int64) {
	o.Status = OpStatusInvalid
	o.ModifyEmpId = modifier
	o.UpdatedAt = util.GetNow()
}

// Inviting 邀请中
func (o *Operator) Inviting(modifier int64) {
	o.Status = OpStatusInviting
	o.ModifyEmpId = modifier
	o.UpdatedAt = util.GetNow()
}

// Delete 删除
func (o *Operator) Delete(modifier int64) {
	o.ModifyEmpId = modifier
	o.DeletedAt = util.GetCurrentTimestamp()
}

// GetStatus 获取状态
func (o *Operator) GetStatus() string {
	switch o.Status {
	case OpStatusInviting:
		return OpStatusInvitingStr
	case OpStatusValid:
		return OpStatusValidStr
	case OpStatusInvalid:
		return OpStatusInvalidStr
	default:
		return ""
	}
}

// GetInnerOpStatus 获取内部运营状态
func GetInnerOpStatus(st string) int8 {
	switch st {
	case OpStatusInvitingStr:
		return OpStatusInviting
	case OpStatusValidStr:
		return OpStatusValid
	case OpStatusInvalidStr:
		return OpStatusInvalid
	default:
		return 0
	}
}

// Update 更新
func (o *Operator) Update(roleId, creator int64) {
	if roleId != o.RoleId {
		o.RoleId = roleId
	}
	if creator != 0 {
		o.ModifyEmpId = creator
	}
	o.UpdatedAt = util.GetNow()
}
